package com.zjmzxfzhl.modules.purchase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.Result;
import com.zjmzxfzhl.common.base.BaseController;
import com.zjmzxfzhl.common.util.FileUtil;
import com.zjmzxfzhl.modules.purchase.controller.dto.PurchaseOrderDetailDto;
import com.zjmzxfzhl.modules.purchase.controller.vo.PurchaseOrderDetailVo;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseOrderDetail;
import com.zjmzxfzhl.modules.purchase.service.PurchaseOrderDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * Controller
 *
 * @author 庄金明
 */
@RestController
@RequestMapping("/purchase/orderDetail")
public class PurchaseOrderDetailController extends BaseController {
    @Autowired
    private PurchaseOrderDetailService purchaseOrderDetailService;

    /**
     * 自定义查询列表
     *
     * @param purchaseOrderDetail
     * @param current
     * @param size
     * @return
     */
    @RequiresPermissions("purchase:orderDetail:list")
    @GetMapping(value = "/list")
    public Result list(PurchaseOrderDetail purchaseOrderDetail, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<PurchaseOrderDetail> pageList = purchaseOrderDetailService.list(new Page<PurchaseOrderDetail>(current, size), purchaseOrderDetail);
        return Result.ok(pageList);
    }

    @RequiresPermissions("purchase:orderDetail:list")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        PurchaseOrderDetail purchaseOrderDetail = purchaseOrderDetailService.getById(id);
        return Result.ok(purchaseOrderDetail);
    }

    /**
     * @param purchaseOrderDetail
     * @return
     * @功能：新增
     */
    @RequiresPermissions("purchase:orderDetail:save")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody PurchaseOrderDetail purchaseOrderDetail) {
        purchaseOrderDetailService.save(purchaseOrderDetail);
        return Result.ok();
    }

    /**
     * @param purchaseOrderDetail
     * @return
     * @功能：修改
     */
    @RequiresPermissions("purchase:orderDetail:update")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody PurchaseOrderDetail purchaseOrderDetail) {
        purchaseOrderDetailService.updateById(purchaseOrderDetail);
        return Result.ok();
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @RequiresPermissions("purchase:orderDetail:delete")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            purchaseOrderDetailService.removeByIds(Arrays.asList(idsArr));
        } else {
            purchaseOrderDetailService.removeById(idsArr[0]);
        }
        return Result.ok();
    }


    @GetMapping(value = "/listBy")
    public Result listBy(PurchaseOrderDetailDto detailDto) {
        List<PurchaseOrderDetailVo> orderDetailVoList = purchaseOrderDetailService.listBy(detailDto);
        return Result.ok(orderDetailVoList);
    }

    /**
     * 批量导出设备审批单详情
     *
     * @return
     */
    @GetMapping(value = "/export")
    public void export(HttpServletResponse response, PurchaseOrderDetailDto detailDto) {
        File exportFile = purchaseOrderDetailService.export(detailDto);
        FileUtil.setDownloadResponse(response, exportFile, exportFile.getName(), true);
    }
}
